home *** CD-ROM | disk | FTP | other *** search
/ SuperHack / SuperHack CD.bin / CODING / GRAPHICS / DEVEL2.ZIP / PLG.C < prev    next >
Encoding:
C/C++ Source or Header  |  1992-04-15  |  2.8 KB  |  115 lines

  1. /* PLG file i/o */
  2.  
  3. /* Written by Bernie Roehl, March 1992 */
  4.  
  5. #include <stdio.h>
  6. #include <string.h>
  7. #include "rend386.h"
  8. #include "plg.h"
  9.  
  10. int load_err = 0;  /* set if an error was encountered during loading */
  11.  
  12. static long xshift = 0, yshift = 0, zshift = 0;
  13. static float xrescale = 1, yrescale = 1, zrescale = 1;
  14.  
  15. void set_loadplg_offset(long x, long y, long z)
  16.     {
  17.     xshift = x;  yshift = y;  zshift = z;
  18.     }
  19.     
  20. void set_loadplg_scale(float x, float y, float z)
  21.     {
  22.     xrescale = x;  yrescale = y;  zrescale = z;
  23.     }
  24.     
  25. OBJECT *load_plg(FILE *in)
  26. {
  27.     OBJECT *obj;
  28.     char tbuff[1000];
  29.     char objname[100], buf1[100], buf2[100];
  30.     int nv, np, i;
  31.                  /* skip blank lines */
  32.     do {
  33.         if (fgets(tbuff, sizeof(tbuff), in) == NULL) return NULL;
  34.         }
  35.     while (tbuff[0] == '\n');
  36.  
  37.     if (sscanf(tbuff, "%s %s %s", objname, buf1, buf2) != 3)
  38.         {  load_err = -1; return NULL;  }
  39.     if ((obj = new_obj(0, nv = atoi(buf1), np = atoi(buf2), objname)) == NULL)
  40.         {  load_err = -2; return NULL;  }
  41.  
  42.     for (i = 0; i < nv; ++i)       /* load in vertices */
  43.         {
  44.         float x, y, z;
  45.         /* skip blank lines */
  46.         do  {
  47.             if (fgets(tbuff, sizeof(tbuff), in) == NULL)
  48.                 { load_err = -4; return NULL; }
  49.             }
  50.         while (tbuff[0] == '\n');
  51.  
  52.         if (sscanf(tbuff, "%f %f %f", &x, &y, &z) != 3)
  53.             { load_err = -5;  return NULL;  }
  54.         add_vertex(obj, ((long) (x*xrescale))+xshift, ((long) (y*yrescale))+yshift,
  55.             ((long) (z*zrescale))+zshift);
  56.         }
  57.  
  58.     for (i = 0; i < np; ++i)   /* load polygons */
  59.         {
  60.         int j, npoints;
  61.         unsigned color;
  62.         POLY *poly;
  63.         char *p;
  64.             /* skip blank lines */
  65.         do {
  66.             if (fgets(tbuff, sizeof(tbuff), in) == NULL)
  67.                 { load_err = -6; return NULL; }
  68.             } while (tbuff[0] == '\n');
  69.  
  70.         color = (unsigned) strtoul(tbuff,&p,0) ; /* req so hex colors usable */
  71.         if ((p = strtok(p, " \t")) == NULL)
  72.             { load_err = -8; return NULL; }
  73.         npoints = atoi(p);
  74.         if ((poly = add_poly(obj, color, npoints)) == NULL)
  75.             { load_err = -9;  return NULL;  }
  76.         for (j = 0; j < npoints; ++j)
  77.             {
  78.             if ((p = strtok(NULL, " \t")) == NULL)
  79.                 { load_err = -9; return NULL; }
  80.             add_point(obj, poly, atoi(p));
  81.             }
  82.         }
  83.     compute_obj(obj);
  84.     load_err = 0;
  85.     return obj;
  86. }
  87.  
  88.  
  89. save_plg(OBJECT *obj, FILE *out)
  90.     {
  91.     int nv, np, i;
  92.     char buff[100];
  93.  
  94.     get_obj_info(obj, &nv, &np, buff, sizeof(buff)-1);
  95.     fprintf(out, "%s %d %d\n", buff, nv, np);
  96.     for (i = 0; i < nv; ++i)
  97.         {
  98.         long x, y, z;
  99.         get_vertex_world_info(obj, i, &x, &y, &z);
  100.         fprintf(out, "%ld %ld %ld\n", x, y, z);
  101.         }
  102.     for (i = 0; i < np; ++i)
  103.         {
  104.         int j, n, verts[1000];
  105.         unsigned c;
  106.         get_poly_info(obj, i, &c, &n, verts, sizeof(verts)/sizeof(int));
  107.         fprintf(out, "%d %d", c & 0x7FFF, n);
  108.         for (j = 0; j < n; ++j) fprintf(out, " %d", verts[j]);
  109.         fprintf(out, "\n");
  110.         }
  111.     return 0;
  112. }
  113.  
  114.  
  115.